<template>
  <el-table
    :data="bookIdInfo"
    style="width: 100%">
    <el-table-column
      prop="bookId"
      label="bookId"
      width="100">
    </el-table-column>
    <el-table-column
      prop="barcode"
      label="book barcode"
      width="180">
      <template v-slot="scope">
        <img
          v-if="scope.row?.barcode"
          :src="baseUrl + scope.row?.barcode"
          class="book-image"
        />
      </template>

    </el-table-column>
    <el-table-column
      prop="isBorrow"
      :formatter="readerFormatter"
      label="borrowed"
      width="100">
    </el-table-column>
    <el-table-column
      prop="isReserve"
      :formatter="readerFormatter"
      label="reserved"
      width="100">
    </el-table-column>
    <el-table-column
      prop="damage"
      :formatter="damageFormatter"
      label="damage"
      width="100">
    </el-table-column>
    <el-table-column
      prop="lost"
      :formatter="damageFormatter"
      label="lost"
      width="100">
    </el-table-column>
    <el-table-column label="setDamage" width="120">
      <template v-slot="scope">
        <el-button
          type="primary"
          value="return"
          @click="setDamage(scope.row)"
          >set</el-button
        >
      </template>
    </el-table-column>
    <el-table-column label="setLost" width="120">
      <template v-slot="scope">
        <el-button
          type="primary"
          value="return"
          @click="setLost(scope.row)"
          >set</el-button
        >
      </template>
    </el-table-column>
    <el-table-column label="delete">
      <template v-slot="scope">
        <el-button
          type="danger"
          value="return"
          @click="deleteBookByBookId(scope.row)"
          >delete</el-button
        >
      </template>
    </el-table-column>
  </el-table>
</template>

<script lang="ts" setup>
import * as bookApi from "@/api/books";
import { onMounted, ref } from "@vue/runtime-core";
import { useRoute } from "vue-router";
import { ElMessage } from "element-plus";
import { BASE_URL } from "@/config/constant";

const baseUrl = BASE_URL + '/barcode/';
const route = useRoute();
const bookIdInfo = ref([]);
const getData = () => {
  bookApi.getBooksIncludeBookIdByIsbn({ isbn: route.params.isbn as string }).then((data) => {
    bookIdInfo.value = data;
  });

}
onMounted(() => {
  getData();
})

const readerFormatter = (row: any, col: any, value: string | number | Date) => {
  return value === 1 ? 'yes' : 'no';
};
const damageFormatter = (row: any, col: any, value: string | number | Date) => {
  return value === 0 ? 'yes' : 'no';
};

const setDamage = (book: any) => {
  bookApi
    .setDamage({
      id: book.bookId,
      status: book.damage == 0 ? 1 : 0
    })
    .then(() => {
      ElMessage.success("set successfully");
      getData();
    })
    .catch(err => {
      ElMessage.error(err);
    });
};
const setLost = (book: any) => {
  bookApi
    .setLost({
      id: book.bookId,
      status: book.lost == 0 ? 1 : 0
    })
    .then(() => {
      ElMessage.success("set successfully");
      getData();
    })
    .catch(err => {
      ElMessage.error(err);
    });
};
const deleteBookByBookId = (book: any) => {
  bookApi
    .deleteBookByBookId({
      bookId: book.bookId,
    })
    .then(() => {
      ElMessage.success("delete successfully");
      getData();
    })
    .catch(err => {
      ElMessage.error(err);
    });
};
</script>

<style scoped>
.el-row {
  margin-top: 100px;
}
.card-bottom {
  padding: 14px;
  text-align: left;
}
.book-publish {
  font-size: 13px;
  color: #999;
}

.book-descriptions {
  margin-top: 13px;
  line-height: 12px;
}

.book-image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.book-info {
  list-style: none;
  text-align: left;
}

.book-info td {
  margin: 10px;
  min-height: 30px;
  line-height: 30px;
}
.book-info td:first-child {
  font-weight: bold;
  min-width: 100px;
}
.book-name td {
  font-size: 32px;
  font-weight: bold;
  line-height: 40px;
}
.button {
  display: inline-block;
}
</style>
